package com.springboot.demo.service;

import com.github.pagehelper.PageHelper;
import com.springboot.demo.entity.Admin;
import com.springboot.demo.mapper.AdminMapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Service
@Transactional
public class AdminServiceImpl implements AdminService{

    @Autowired
    private AdminMapper adminMapper;

    @Override
    public Admin login(String telephone, String password) {
        return adminMapper.login(telephone,password);
    }

    @Override
    public Admin register(@Param("nickname") String nickname
            ,@Param("telephone") String telephone
            ,@Param("password") String password
            ,@Param("identifiedPassword") String identifiedPassword) {
        //此时说明该用户已经存在
        if(adminMapper.regiSuccess(nickname,telephone,password)!=null){
            return null;
        }else{
            if(accountValidity( nickname,telephone,password,identifiedPassword)){
                adminMapper.register(nickname, telephone, password);
                return adminMapper.regiSuccess(nickname, telephone, password);
            }else{
                return null;
            }
        }
    }

    @Override
    public String regiLibrarian(@Param("nickname") String nickname
            ,@Param("telephone") String telephone
            ,@Param("password") String password) {
        if(adminMapper.check(telephone)!=null){
            return "phone exists";
        }else{
            String ret = another_accountValidity( nickname,telephone,password);
            if(ret.equals("success")){
                adminMapper.insertLibrarianAccount(nickname, telephone, password);
                if(adminMapper.regiSuccess(nickname, telephone, password)!=null)
                    return "success";
                return "something goes wrong";
            }else{
                return ret;
            }
        }
    }

    @Override
    public boolean accountValidity(String nickname,String telephone,String password,String identifiedPassword) {
        if(nickname=="" || telephone=="" || password==""){
            return false;
        }
        if(nickname == null || telephone == null || password == null){
            return false;
        }
        if(nickname.length()<6){
            return false;
        }
        Pattern pattern= Pattern.compile("[0-9]*");//正则表达式
        Matcher isNum=pattern.matcher(telephone);
        if(!isNum.matches() || telephone.length()<11) {
            return false;
        }
        if(password.length()<6){
            return false;
        }
        if(!identifiedPassword.equals(password)){
            return false;
        }
        return true;
    }

    @Override
    public String another_accountValidity(String nickname,String telephone,String password)
    {
        if(nickname == null || telephone == null || password == null){
            return "something is null";
        }
        if(nickname.length()<2){
            return "name short";
        }
        Pattern pattern= Pattern.compile("[0-9]*");//正则表达式
        Matcher isNum=pattern.matcher(telephone);
        if(!isNum.matches() || telephone.length()<11)
            return "phone err";
        if(password.length()<6)
            return "password short";
        return "success";
    }

    @Override
    public boolean changeAccount( String nickname, String phonenumber, String password) {
        if(nickname=="" || phonenumber=="" || password==""){
            return false;
        }
        if(nickname==null || phonenumber==null || password==null){
            return false;
        }else {
            if(adminMapper.hasAccount(phonenumber)==null) {
                adminMapper.insertAdminAccount(nickname, phonenumber, password);
            }else{
                // 可以改得和现在一样。。。。
                adminMapper.deleteAccount(phonenumber);
                adminMapper.insertAdminAccount(nickname, phonenumber, password);
            }
            return true;
        }
    }

    @Override
    public Admin delete(String phone) {
        if (adminMapper.check(phone)!=null)
            adminMapper.delete(phone);
        return adminMapper.check(phone);//null if success
    }

    @Override
    public Admin modify(String phone, String phone1, String lname, String password) {

        if(adminMapper.check(phone)==null){
            return null;
        }else{
            if(lname!=null && phone1!=null && password!=null && phone!=null){
                adminMapper.modify_pn(phone,lname,password);
                return adminMapper.check(phone1);
            }else{
                return null;
            }
        }
    }

    @Override
    public List<Admin> getAll(int page,int limit)
    {
        PageHelper.startPage(page,limit);
        System.out.println(page+" "+limit);
        return adminMapper.selectAll();
    }

    public void modify_base(String name,int value)
    {
        adminMapper.modify_base(name,value);
    }
}
